<template>
  <!-- 外发详情-->
  <div id="productionOutDetail">
    <el-form :model="form" label-width="100">
      <el-form-item label="客户名称:">
        <p>{{ form.customerName }}</p>
      </el-form-item>
      <el-form-item label="账期:">
        <p>{{ form.startTime }}--{{ form.endTime }}的账单</p>
      </el-form-item>
      <el-form-item label="合计总金额:">
        <p>{{ form.planMoney }}元</p>
      </el-form-item>
    </el-form>
    <el-table
      ref="table"
      :data="form.quotesProductsList"
      :span-method="objectSpanMethod"
    >
      <el-table-column
        label="合同编码"
        prop="contractCode"
        min-width="150"
      ></el-table-column>
      <el-table-column
        label="签订时间"
        prop="orderTime"
        width="150"
      ></el-table-column>
      <el-table-column
        label="合同金额"
        prop="contractMoney"
        width="150"
      ></el-table-column>
      <el-table-column
        label="产品名称"
        prop="name"
        min-width="100"
      ></el-table-column>
      <el-table-column
        label="规格型号"
        prop="specification"
        min-width="100"
      ></el-table-column>
      <el-table-column
        label="产品数量"
        prop="num"
        width="150"
      ></el-table-column>
      <el-table-column
        label="产品单位"
        prop="unitName"
        width="150"
      ></el-table-column>
    </el-table>
  </div>
</template>
<script>
export default {
  name: "productionOutDetail",
  data () {
    return {
      columnsFilters: ['contractMoney'],
    }
  },
  props: {
    form: {
      type: Object,
      required: true,
      default: () => { },
    },
  },
  methods: {
    objectSpanMethod ({ row, column, rowIndex, columnIndex }) {
      let data = this.form.quotesProductsList
      let noSortArr = ["contractCode", "contractMoney", "orderTime"]
      if (noSortArr.includes(column.property)) {
        let prevRow = data[rowIndex - 1]
        let nextRow = data[rowIndex + 1]
        let str = ""
        noSortArr.forEach((item) => {
          str += row[item]
        })
        if (typeof prevRow != "undefined") {
          let str2 = ""
          noSortArr.forEach((item) => {
            str2 += prevRow[item]
          })
          if (prevRow && str === str2) {
            //当有上一条数据，并且和当前值相等时
            return { rowspan: 0, colspan: 0 }
          }
          // else {
          //   let countRowspan = 1;
          //   while (nextRow && str === str3) {
          //     //当有下一条数据并且和当前值相等时,获取新的下一条
          //     nextRow = data[++countRowspan + rowIndex];
          //     str3 = "";
          //     if (typeof nextRow != "undefined") {
          //       noSortArr.forEach((item) => {
          //         str3 += nextRow[item];
          //       });
          //     }
          //   }
          //   if (countRowspan > 1) {
          //     return { rowspan: countRowspan, colspan: 1 };
          //   }
          // }
        }
        if (typeof nextRow != "undefined") {
          let str3 = ""
          noSortArr.forEach((item) => {
            str3 += nextRow[item]
          })
          let countRowspan = 1
          while (nextRow && str === str3) {
            //当有下一条数据并且和当前值相等时,获取新的下一条
            nextRow = data[++countRowspan + rowIndex]
            str3 = ""
            if (typeof nextRow != "undefined") {
              noSortArr.forEach((item) => {
                str3 += nextRow[item]
              })
            }
          }
          if (countRowspan > 1) {
            return { rowspan: countRowspan, colspan: 1 }
          }
        }
      }
    },
    //合计行统计
    getSummaries (param) {
      const { columns, data } = param
      const sums = []
      columns.forEach((column, index) => {
        if (index === 1) {
          sums[index] = "页合计"
          return
        }
        if (this.columnsFilters.includes(column.property)) {
          const values = data.map((item) => Number(item[column.property]))
          if (!values.every((value) => isNaN(value))) {
            sums[index] = values.reduce((prev, curr) => {
              const value = Number(curr)
              if (!isNaN(value)) {
                return prev + curr
              } else {
                return prev
              }
            }, 0)
            sums[index] += " 元"
          } else {
            sums[index] = ""
          }
        }
      })
      return sums
    },
  },
};
</script>
<style lang="scss" scoped>
.button {
  margin: 10px 0 0 40px;
}
</style>
